package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.persistence.SignInDAO;
import org.csu.mypetstore.persistence.impl.SignInDAOImpl;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.SignInService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SignInServlet extends HttpServlet {
    private Account account;
    private String username;
    private String password;
    private static final String BACK_TO_SIGNONFORM = "/WEB-INF/jsp/account/SignonForm.jsp";
    private static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SignInService signInService = new SignInService();
        HttpSession session = request.getSession();
        username = request.getParameter("username");
        password = request.getParameter("password");
        account = signInService.getAccount(username,password);

        //获得输入的验证码值
        String value1=request.getParameter("vCode");
        /*获取图片的值*/
        String value2=(String)session.getAttribute("checkcode");
        Boolean isSame = false;

        //转为小写
        value1 = value1.toLowerCase();
        value2 = value2.toLowerCase();

        //从session获取滑块
        Boolean isSuccess = Boolean.parseBoolean(request.getParameter("isSuccess"));
        session.setAttribute("username",username);
        /*对比两个值（字母不区分大小写）*/
        if(value2.equals(value1)){
            isSame = true;
        }

        if (account == null|| !isSame){
            if(!isSame){
                request.setAttribute("loginMsg","验证码错误");
                request.getRequestDispatcher(BACK_TO_SIGNONFORM).forward(request,response);}
            else{
                request.setAttribute("loginMsg","用户名或密码错误");
                request.getRequestDispatcher(BACK_TO_SIGNONFORM).forward(request,response);
            }
        }else {
            account.setPassword(null);
            session.setAttribute("account",account);
            /**************************日志********************************/
            if(account != null){
                HttpServletRequest httpRequest= request;
                String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
                        + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + strBackUrl + " Login";
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            /***********************************************************/
            request.getRequestDispatcher(MAIN).forward(request,response);
        }
    }
}
